package com.ccp.dev.qbdms.tag;

import com.ccp.dev.core.util.AppUtil;
import com.ccp.dev.system.model.Dictionary;
import com.ccp.dev.system.service.DictionaryService;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import java.util.List;

public class DicTag extends SimpleTagSupport {

    /**
     * 下拉列表控件的名称
     */
    private String id;

    /**
     * 下拉列表控件的名称
     */
    private String name;

    /**
     * 下拉列表控件的名称
     */
    private String css;

    /**
     * 下拉列表的宽度
     */
    private Integer width = 0;

    /**
     * 数据字典nodeKey
     */
    private String nodeKey;

    /**
     * 当前需要选中的key
     */
    private String nowItemValue;

    /**
     * 是否可选
     */
    private Boolean activity = true;

    /**
     * 是否增加请选择
     */
    private Boolean addSelectText = true;

    /**
     * 验证
     */
    private String verify;

    /**
     * 提示信息
     */
    private String reqtext;

    /**
     * 子表必填
     */
    private String z_verify;

    /**
     * 验证
     */
    private Boolean search = false;

    /**
     * 增加lay-filter
     */
    private String filter;


    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCss() {
        return css;
    }

    public void setCss(String css) {
        this.css = css;
    }

    public String getNodeKey() {
        return nodeKey;
    }

    public void setNodeKey(String nodeKey) {
        this.nodeKey = nodeKey;
    }

    public String getNowItemValue() {
        return nowItemValue;
    }

    public void setNowItemValue(String nowItemValue) {
        this.nowItemValue = nowItemValue;
    }

    public void setWidth(Integer width) {
        this.width = width;
    }

    public Integer getWidth() {
        return width;
    }

    public Boolean getActivity() {
        return activity;
    }

    public void setActivity(Boolean activity) {
        this.activity = activity;
    }

    public String getVerify() {
        return verify;
    }

    public void setVerify(String verify) {
        this.verify = verify;
    }

    public String getReqtext() {
        return reqtext;
    }

    public void setReqtext(String reqtext) {
        this.reqtext = reqtext;
    }

    public Boolean getAddSelectText() {
        return addSelectText;
    }

    public void setAddSelectText(Boolean addSelectText) {
        this.addSelectText = addSelectText;
    }

    public Boolean getSearch() {
        return search;
    }

    public void setSearch(Boolean search) {
        this.search = search;
    }

    public String getFilter() {
        return filter;
    }

    public void setFilter(String filter) {
        this.filter = filter;
    }

    public String getZ_verify() {
        return z_verify;
    }

    public void setZ_verify(String z_verify) {
        this.z_verify = z_verify;
    }

    @Override
    public void doTag() throws JspException {
        JspWriter out = getJspContext().getOut();
        try {
            DictionaryService dictionaryService = AppUtil.getBean(DictionaryService.class);
            List<Dictionary> list = dictionaryService.getByNodeKey(nodeKey);

            StringBuffer content = new StringBuffer("<select ");
            if (id != null) {
                content.append("id=\"" + id + "\" ");
            }
            if (name != null) {
                content.append("name=\"" + name + "\" ");
            }
            if (css != null) {
                content.append("class=\"" + css + "\" ");
            }
            if (width != 0) {
                content.append("style=\"width:" + width + "px\" ");
            }
            if (!activity) {
                content.append(" disabled ");
            }
            if (verify != null) {
                content.append(" lay-verify=\"" + verify + "\" ");
            }
            if (reqtext != null) {
                content.append(" lay-reqtext=\"" + reqtext + "\" ");
            }
            if (z_verify != null) {
                content.append(" requiredFlag=\"" + z_verify + "\" ");
            }
            if (search) {
                content.append(" lay-search ");
            }
            if (filter != null) {
                content.append(" lay-filter= \"" + filter + "\" ");
            }
            content.append(">");
            if (addSelectText) {
                content.append("<option value=\"\"></option>");
            }
            for (Dictionary dic : list) {
                content.append("<option value=" + dic.getItemValue());
                if (dic.getItemValue().equals(nowItemValue)) {
                    content.append(" selected ");
                }
                content.append(">" + dic.getItemName() + "</option>");
            }
            content.append("</select>");
            out.print(content);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
